﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EraNet.Core.Model.Messages;
using EraNet.Services.Messages;

namespace EraNet.SysAdmin.Controllers
{
    public class MessagesController : Controller
    {
        #region Fields
        private readonly INoticeServices _inoticeServices;
        #endregion

        #region Constructors
        public MessagesController(INoticeServices _NoticeServices)
        {
            this._inoticeServices = _NoticeServices;
        }
        #endregion
        //
        // GET: /Messages/

        public ActionResult NoticeList()
        {
            return View();
        }
        /// <summary>
        /// 获取公告列表
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public JsonResult AjaxNoticeList(int? page)
        {
            int pageIndex = page ?? 1;
            var list = _inoticeServices.SelectPager(pageIndex, 10);
            return Json(new { total = list.TotalCount, list = list }, JsonRequestBehavior.AllowGet);
        }

        //public ActionResult NoticeInfo(int? id)
        //{
        //    Notices m = new Notices();
        //    if (id != null)
        //    {
        //        int rid = 0;
        //        if (int.TryParse(id.ToString(), out rid))
        //        {
        //            m = _inoticeServices.GetNoticeById(rid);
        //        }
        //    }
        //    return View(m);
        //}

        public JsonResult getmodel(int? id)
        {
            Notices m = new Notices();
            if (id != null)
            {
                int rid = 0;
                if (int.TryParse(id.ToString(), out rid))
                {
                    m = _inoticeServices.GetNoticeById(rid);
                }
            }
            return Json(new {model=m },JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="ot"></param>
        /// <returns></returns>
        public JsonResult addnotice(int? id, string ot)
        {
            Notices m = new Notices();
            #region   数据
            bool rs = false;
            string rmsg = "操作失败！";
            string title = Request.Form["txt_title"];
            string acceptid = Request.Form["txt_acpid"];
            DateTime start = Convert.ToDateTime(Request.Form["dtf_start"]);
            DateTime end = Convert.ToDateTime(Request.Form["dtf_end"]);
            string constr = Request.Form["hdt_con"];
            #endregion
            if (id == null)
            {
                m.AcceptId = acceptid;
                m.ContentStr = constr;
                m.CreateDate = DateTime.Now;
                m.EndDate = end;
                m.NType1 = 1;
                m.StartDate = start;
                m.Title = title;
                if (_inoticeServices.Insert(m) > 0)
                {
                    rs = true;
                    rmsg = "操作成功！";
                }
            }
            else
            {
                int realid = Convert.ToInt32(id);
                m = _inoticeServices.GetNoticeById(realid);
                m.AcceptId = acceptid;
                m.ContentStr = constr;
                m.EndDate = end;
                m.StartDate = start;
                m.Title = title;
                if (_inoticeServices.Update(m) > 0)
                {
                    rs = true;
                    rmsg = "操作成功！";
                }

            }
            return Json(new { success = rs, msg = rmsg }, JsonRequestBehavior.AllowGet);
        }

    }
}
